class CommentsController < ApplicationController
    
  # GET /comments
  # GET /comments.xml
  def index
    @comments = Comment.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comments }
      format.atom
    end
  end

  # GET /comments/1
  # GET /comments/1.xml
  def show
    @comment = Comment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @comment }
    end
  end

  # GET /comments/new
  # GET /comments/new.xml
  def new
    @comment = Comment.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @comment }
    end
  end

  # GET /comments/1/edit
  def edit
    @comment = Comment.find(params[:id])
  end

  # POST /comments
  # POST /comments.xml
  def create
    @comment = Comment.new(params[:comment])
    
    if @user 
      @comment.email = @user.email
      @comment.name = @user.name
      @comment.user_id = @user.id
    end
    
    return if (Time.now > (@comment.post.published_at + @comment.post.comments_disabled_after.days ))

    unless APP_SETTINGS[:askimet_key] == nil
      include Akismet 
      if is_spam?(:comment_content => @comment.body, 
               :comment_type => "comment", 
               :comment_author => @comment.name, 
               :comment_author_email => @comment.email,
               :user_ip => request.remote_ip
               )
        @comment.is_spam = true
      end
    end
    
    if APP_SETTINGS[:comment_throttling] > 0     
      existing_logs = Log.find(:all, :conditions => ["remote_ip = ? and created_at > ? and controller = 'comments' and action = 'create'", request.remote_ip, APP_SETTINGS[:comment_throttling].seconds.ago ])
      logger.info existing_logs.inspect
      if existing_logs.length > 0 
        @comment.errors.add_to_base("You may not comment so quickly after your previous comment, please wait a moment and try again.")     
        logger.info "Comment throttled from #{request.remote_ip}"
      end
    end
      
    respond_to do |format|
      if @comment.errors.blank? and @comment.save
        flash[:notice] = 'Comment was successfully created.'
        
        log_event("Comment created", @comment.id)
        
        format.js
        format.html { redirect_to(:controller => :posts, :action => :show, :id => @comment.post_id) }
        format.xml  { render :xml => @comment, :status => :created, :location => @comment }
      else
        format.js 
        format.html { render :action => "new" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /comments/1
  # PUT /comments/1.xml
  def update
    @comment = Comment.find(params[:id])

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        
        log_event("Comment updated", @comment.id)
        
        flash[:notice] = 'Comment was successfully updated.'
        format.html { redirect_to(@comment) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.xml
  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    log_event("Comment destroyed", @comment.id)

    respond_to do |format|
      format.html { redirect_to(comments_url) }
      format.xml  { head :ok }
    end
  end
end
